package com.zzy.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.edu.client.VodClient;
import com.zzy.edu.entity.EduVideo;
import com.zzy.edu.mapper.EduVideoMapper;
import com.zzy.edu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author KAZU
 * @since 2023-01-13
 */
@SuppressWarnings("all")
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
	
	@Autowired
	private EduVideoMapper videoMapper;
	
	@Autowired
	private VodClient client;
	
	/**
	 * 根据课程id删小节&视频
	 * */
	@Override
	public void removeVideoByCourseId(String courseId) {
		QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("course_id", courseId);
		queryWrapper.select("video_source_id");
		List<EduVideo> videoList = videoMapper.selectList(queryWrapper);
		List<String> list = new ArrayList<>();
		for (EduVideo video :videoList) {
			if (!StringUtils.isEmpty(video.getVideoSourceId())) {
				list.add(video.getVideoSourceId());
			}
			
		}
		
		if (list.size() > 0) {
			client.deleteBatch(list);
		}
		
		QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id", courseId);
		videoMapper.delete(wrapper);
	}
}
